package com.newtribe.access;

import java.net.InetAddress;
import java.net.ServerSocket;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: ShenYang newtribe.external Corp .</p>
 * @author fuwei ,fuw@newtribe.external.com
 * @version 1.0
 */

public class BridgeServer extends Server{

  private int timout = 0 ;
  private int MAX_PROCESSORS = 5 ;
  private int port = 8080 ;
  public static final String  SERVER_NAME ="Newtribe Bridge Server" ;


  /**
   * get timeout times
   * @return
   */
  public  int getTimeOut() {
    return  this.timout ;
  }
  /**
   * get max processors
   * @return
   */
  protected  int getProcessors() {
    return this.MAX_PROCESSORS ;
  }
  /**
   * get configed listen port
   * @return
   */
  public  int getListenPort() {
    return this.port ;
  }
  /**
   * get concrete service name .
   * @return
   */
  public  String getServiceName() {
    return SERVER_NAME ;
  }

  /**
   *  create a new Processor class .
   */
  public  Processor newProcessor() {
    return new BridgeProcessor(this ,sequence) ;
  }
  
  public void threadDoStart() throws Exception {
	    //we have start the server .
	    if (serverSocket != null)
	      return;
	    try {

//	      serverSocket = new ServerSocket( getListenPort() ,0 ,InetAddress.getByName("0.0.0.0")
//	                                       );
	      serverSocket=SSLServerManager.getServerSocket(this.port) ;
	      System.out.println(getServiceName() +"Server Start ....");
	      System.out.println("Processor [" + this.getProcessors()  + "]");
	      System.out.println("Listen port [" + getListenPort() + "]");
	      stop = false;
	      //initialize processors
	      for (int i = 0; i < getProcessors(); i++) {
	        Processor  processor = newProcessor();
	        processor.start() ;
	        free.add(processor) ;
	        sequence ++ ;
	      }

	      mainLoop();

	    }
	    catch (Exception e) {
	      e.printStackTrace() ;
	      log.fatal("an error occured ,can't startup normally!");
	      stop();
	    }

	  }


}